clean_get_addresses<-function(years_bac,years_adm){
########################BAC########################################
  #obtaining town name from the school name; algorithm is not perfect;
  #I simply remove keywords such as "school" and everything between quotes and hope 
  #that what is left is the town name
  eval(parse('clean_get_addresses__get_town_from_list.R', encoding = 'UTF-8'))
  
#Get unique HS form BAC
  for (i in years_bac){
    print(i)
    setwd(wd_data_intermediate)
    graduation_file_final<-paste("data_bac_raw",i,sep="")
    data_temp<-readRDS(paste(graduation_file_final,".rds",sep=""))

    data_temp<-unique(data_temp[,c("judet_bac","unitate_de_invatamant")])

    if (i==years_bac[1]){
      hs_bac<-data_temp
    } else {
      hs_bac<-rbind(hs_bac,data_temp)
    }
    hs_bac<-unique(hs_bac[,c("judet_bac","unitate_de_invatamant")])
    rm(data_temp)
    gc()
  }

  #Get locations
  hs_bac<-hs_bac %>% rename(school_name=unitate_de_invatamant)
  hs_bac<-hs_bac %>% filter(school_name!='')
  

  hs_bac$address<-NA
  hs_bac$lat<-NA
  hs_bac$lng<-NA
  hs_bac$match_type<-NA
  
  hs_bac$school_name_modified<-gsub('"(.*?)"',"",hs_bac$school_name,perl=T)
  
  hs_bac$school_name_town<-gsub('"(.*?)"',"",hs_bac$school_name,perl=T)
  replace_string<-paste0(".+( TEHNOLOGIC DE TURISM SI ALIMENTATIE )|.+( MARMATIA )|.+( AL BANATULUI MONTAN )|",
                         ".+( PARTICULAR )|.+( DE ARTA )|.+( DEFICIENTI DE VEDERE )|.+( BILINGV ROMINO-CROAT )|",
                         ".+( DE CONSTRUCTII SI PROTECTIA MEDIULUI )|.+( PROFESIONALA DE COOPERATIE )|",
                         ".+( DE PROTECTIA MEDIULUI  )|.+( WALDORF )|(FEG )|.+( DE VEST )|",
                         ".+( PENTRU EDUCATIE INCLUZIVA )|.+( DE ARTE SI MESERII )|",
                         ".+( CONSTR. CAI FERATE )|.+( DE CHIMIE INDUSTRIALA )|",
                         ".+( CONSTRUCTII MONTAJ )|.+( INDUSTRIAL MINIER )|.+( MINIER )|",
                         ".+( DE COOPERATIE )|.+( INDUSTRIE USOARA )|.+( ECONOMIC-ADMINISTRATIV)|",
                         ".+( MESERII SI SERVICII )|.+( INDUSTRIE MICA SI SERVICII )|.+( NATIONALA DE GAZ )|",
                         ".+( DE MUZICA SI ARTE PLASTICE )|.+( CU PROGRAM DE ATLETISM )|.+( DE MUZICA )|",
                         ".+( ADMINISTRATIV SI DE SERVICII )|.+( ALIMENTATIE PUBLICA )|.+( CU PROGRAM SPORTIV )|",
                         ".+( FORESTIER )|.+( INDUSTRIA STICLEI )|.+( COOPERATIST )|.+( COOPERATIE )|",
                         ".+( DE MARINA )|.+( DE AGRICULTURA SI ECONOMIE )|.+( CONSTRUCTII CAI FERATE )|",
                         ".+( DE TRANSPORTURI AUTO )|.+( BILINGV ROMANO-CROAT )|.+( TEHNIC DE TRANSPORTURI )|",
                         ".+( SPECIAL PENTRU DEFICIENTI DE AUZ )|.+( ADVENTIST )|",
                         ".+( TRANSPORTURI CAI FERATE )|.+( VOCATIONAL DE ARTA )|.+( ROMANO-CATOLIC )|.+( SPECIAL )|.+( AUTO )|",
                         ".+( SILVIC )|.+( TEHNIC )|.+( CONSTRUCTII DE MASINI )|.+( METALURGIC )|.+( PETROL )|",
                         ".+( BANATEAN )|.+( CARASAN )|.+( ALIMENTARA )|.+( PENTICOSTAL )|.+( BAPTIST )|.+( AGRICOL )|.+( DE AFACERI )|",
                         ".+( REFORMAT )|.+( TEOLOGIC )|.+( ORTODOX )|.+( TEHNOLOGIC FORESTIER )|.+( TEHNOLOGIC CONSTRUCTII DE MASINI )|",
                         ".+( SPORTIV )|.+( SANITAR )|.+( ENERGETIC )|.+( TEHNOLOGIC )|.+( PEDAGOGIC )|.+( TEORETIC )|",
                         ".+( DE INFORMATICA )|.+( ECONOMIC )|.+( TRANSPORTURI )|.+( ADMINISTRATIV )|.+( DE TURISM )|.+( PENTRU TRANSPORT RUTIER )|",
                         ".+( MAGHIARA )|.+( TELECOMUNICATII )|.+( DE ARTE )|.+( CONSTRUCTII )|.+( SERVICII )|.+( AUTOMECANICA )|.+( NATIONAL )|",
                         ".+( FEROVIAR )|.+( TEXTIL )|(GR. SC. )|(CENTRUL SCOLAR )|(GRUP SCOLAR INDUSTRIAL )|",
                         ".+(THE CAMBRIDGE INTERNATIONAL SCHOOL IN )|.+( DIN )|(GRI )|(GRUP SCOLAR )|(GRUPUL SCOLAR INDUSTRIAL )|(GRUPUL SCOLAR )|(LICEUL )") 
  hs_bac$school_name_town<-gsub(replace_string,"",hs_bac$school_name_town)
  hs_bac$school_name_town<-paste(hs_bac$school_name_town,"Romania",sep=", ")
  
  

  for (j in 1:dim(hs_bac)[1]){
    tryCatch({
      if(j%%100==0){
        print(j)
      }
      temp<-google_find_place(hs_bac$school_name[j], fields = c("formatted_address","geometry"))
      #if no initial match
      if (is.null(temp$candidates$formatted_address)){

        temp<-google_find_place(hs_bac$school_name_long[j], fields = c("formatted_address","geometry"))
        if (is.null(temp$candidates$formatted_address)){
          temp<-google_find_place(hs_bac$school_name_town[j], fields = c("formatted_address","geometry"))
          if (is.null(temp$candidates$formatted_address)){
            hs_bac[j,]$match_type<-'unmatched'
            hs_bac[j,]$address<-NA
            hs_bac[j,]$lat<-NA
            hs_bac[j,]$lng<-NA
          } else {
            hs_bac[j,]$match_type<-'matched using only town name'
            hs_bac[j,]$address<-temp$candidates$formatted_address[1]
            hs_bac[j,]$lat<-temp$candidates$geometry$location$lat[1]
            hs_bac[j,]$lng<-temp$candidates$geometry$location$lng[1]
          }
        } else {
          hs_bac[j,]$match_type<-'matched using modified name'
          hs_bac[j,]$address<-temp$candidates$formatted_address[1]
          hs_bac[j,]$lat<-temp$candidates$geometry$location$lat[1]
          hs_bac[j,]$lng<-temp$candidates$geometry$location$lng[1]
        }
      } else {
        hs_bac[j,]$match_type<-'matched using name'
        hs_bac[j,]$address<-temp$candidates$formatted_address[1]
        hs_bac[j,]$lat<-temp$candidates$geometry$location$lat[1]
        hs_bac[j,]$lng<-temp$candidates$geometry$location$lng[1]
      }
    }, error=function(e){
      print("error")
      Sys.sleep(5)
      temp<-google_find_place(hs_bac$school_name[j], fields = c("formatted_address","geometry"))
      #if no initial match
      if (is.null(temp$candidates$formatted_address)){

        temp<-google_find_place(hs_bac$school_name_long[j], fields = c("formatted_address","geometry"))
        if (is.null(temp$candidates$formatted_address)){

          temp<-google_find_place(hs_bac$school_name_town[j], fields = c("formatted_address","geometry"))
          if (is.null(temp$candidates$formatted_address)){
            hs_bac[j,]$match_type<-'unmatched'
            hs_bac[j,]$address<-NA
            hs_bac[j,]$lat<-NA
            hs_bac[j,]$lng<-NA
          } else {
            hs_bac[j,]$match_type<-'matched using only town name'
            hs_bac[j,]$address<-temp$candidates$formatted_address[1]
            hs_bac[j,]$lat<-temp$candidates$geometry$location$lat[1]
            hs_bac[j,]$lng<-temp$candidates$geometry$location$lng[1]
          }
        } else {
          hs_bac[j,]$match_type<-'matched using name and county'
          hs_bac[j,]$address<-temp$candidates$formatted_address[1]
          hs_bac[j,]$lat<-temp$candidates$geometry$location$lat[1]
          hs_bac[j,]$lng<-temp$candidates$geometry$location$lng[1]
        }
      } else {
        hs_bac[j,]$match_type<-'matched using name'
        hs_bac[j,]$address<-temp$candidates$formatted_address[1]
        hs_bac[j,]$lat<-temp$candidates$geometry$location$lat[1]
        hs_bac[j,]$lng<-temp$candidates$geometry$location$lng[1]
      }
    })
  }
  setwd(wd_data_final)
  saveRDS(hs_bac, file = "hs_bac_locations")
  hs_bac<-readRDS("hs_bac_locations")



  ###########################ADM#####################################  
  #Get unique HS and MS from ADM that are not in BAC and 
  for (i in years_adm){
    print(i)
    graduation_file_final<-paste("data_adm_raw",i,sep="")
    
    setwd(wd_data_intermediate)
    data_temp<-readRDS(paste(graduation_file_final,".rds",sep=""))
    data_temp_adm_liceu<-unique(data_temp[,c("judet","liceu_repartizat")])
    data_temp_adm_scoala<-unique(data_temp[,c("judet","scoala_de_provenienta")])

    if (i==years_adm[1]){
      hs_adm_all<- data_temp_adm_liceu
      ms_adm_all<-data_temp_adm_scoala
    } else {
      hs_adm_all<-rbind(hs_adm_all,data_temp_adm_liceu)
      ms_adm_all<-rbind(ms_adm_all,data_temp_adm_scoala)
    }
    rm(data_temp,data_temp_adm_liceu,data_temp_adm_scoala)

    hs_adm_all<-unique(hs_adm_all[,c("judet","liceu_repartizat")])
    ms_adm_all<-unique(ms_adm_all[,c("judet","scoala_de_provenienta")])

    hs_adm<-anti_join(hs_adm_all,hs_bac,by=c("liceu_repartizat"="school_name","judet"))
    ms_adm<-anti_join(ms_adm_all,hs_bac,by=c("scoala_de_provenienta"="school_name","judet"))
    ms_adm<-anti_join(ms_adm,hs_adm_all,by=c("scoala_de_provenienta"="liceu_repartizat","judet"))
    gc()
  }


  
  hs_adm<-hs_adm %>% rename(school_name=liceu_repartizat)
  hs_adm<-hs_adm %>% filter(school_name!='')

  hs_adm$school_name_modified<-gsub('"(.*?)"',"",hs_adm$school_name,perl=T)
  
  hs_adm$school_name_town<-gsub('"(.*?)"',"",hs_adm$school_name,perl=T)
  replace_string<-paste0(".+( TEHNOLOGIC DE TURISM SI ALIMENTATIE )|.+( MARMATIA )|.+( AL BANATULUI MONTAN )|",
                         ".+( PARTICULAR )|.+( DE ARTA )|.+( DEFICIENTI DE VEDERE )|.+( BILINGV ROMINO-CROAT )|",
                         ".+( DE CONSTRUCTII SI PROTECTIA MEDIULUI )|.+( PROFESIONALA DE COOPERATIE )|",
                         ".+( DE PROTECTIA MEDIULUI  )|.+( WALDORF )|(FEG )|.+( DE VEST )|",
                         ".+( PENTRU EDUCATIE INCLUZIVA )|.+( DE ARTE SI MESERII )|",
                         ".+( CONSTR. CAI FERATE )|.+( DE CHIMIE INDUSTRIALA )|",
                         ".+( CONSTRUCTII MONTAJ )|.+( INDUSTRIAL MINIER )|.+( MINIER )|",
                         ".+( DE COOPERATIE )|.+( INDUSTRIE USOARA )|.+( ECONOMIC-ADMINISTRATIV)|",
                         ".+( MESERII SI SERVICII )|.+( INDUSTRIE MICA SI SERVICII )|.+( NATIONALA DE GAZ )|",
                         ".+( DE MUZICA SI ARTE PLASTICE )|.+( CU PROGRAM DE ATLETISM )|.+( DE MUZICA )|",
                         ".+( ADMINISTRATIV SI DE SERVICII )|.+( ALIMENTATIE PUBLICA )|.+( CU PROGRAM SPORTIV )|",
                         ".+( FORESTIER )|.+( INDUSTRIA STICLEI )|.+( COOPERATIST )|.+( COOPERATIE )|",
                         ".+( DE MARINA )|.+( DE AGRICULTURA SI ECONOMIE )|.+( CONSTRUCTII CAI FERATE )|",
                         ".+( DE TRANSPORTURI AUTO )|.+( BILINGV ROMANO-CROAT )|.+( TEHNIC DE TRANSPORTURI )|",
                         ".+( SPECIAL PENTRU DEFICIENTI DE AUZ )|.+( ADVENTIST )|",
                         ".+( TRANSPORTURI CAI FERATE )|.+( VOCATIONAL DE ARTA )|.+( ROMANO-CATOLIC )|.+( SPECIAL )|.+( AUTO )|",
                         ".+( SILVIC )|.+( TEHNIC )|.+( CONSTRUCTII DE MASINI )|.+( METALURGIC )|.+( PETROL )|",
                         ".+( BANATEAN )|.+( CARASAN )|.+( ALIMENTARA )|.+( PENTICOSTAL )|.+( BAPTIST )|.+( AGRICOL )|.+( DE AFACERI )|",
                         ".+( REFORMAT )|.+( TEOLOGIC )|.+( ORTODOX )|.+( TEHNOLOGIC FORESTIER )|.+( TEHNOLOGIC CONSTRUCTII DE MASINI )|",
                         ".+( SPORTIV )|.+( SANITAR )|.+( ENERGETIC )|.+( TEHNOLOGIC )|.+( PEDAGOGIC )|.+( TEORETIC )|",
                         ".+( DE INFORMATICA )|.+( ECONOMIC )|.+( TRANSPORTURI )|.+( ADMINISTRATIV )|.+( DE TURISM )|.+( PENTRU TRANSPORT RUTIER )|",
                         ".+( MAGHIARA )|.+( TELECOMUNICATII )|.+( DE ARTE )|.+( CONSTRUCTII )|.+( SERVICII )|.+( AUTOMECANICA )|.+( NATIONAL )|",
                         ".+( FEROVIAR )|.+( TEXTIL )|(GR. SC. )|(CENTRUL SCOLAR )|(GRUP SCOLAR INDUSTRIAL )|",
                         ".+(THE CAMBRIDGE INTERNATIONAL SCHOOL IN )|.+( DIN )|(GRI )|(GRUP SCOLAR )|(GRUPUL SCOLAR INDUSTRIAL )|(GRUPUL SCOLAR )|(LICEUL )") 
  hs_adm$school_name_town<-gsub(replace_string,"",hs_adm$school_name_town)
  hs_adm$school_name_town<-paste(hs_adm$school_name_town,"Romania",sep=", ")  
 

  ### Get HS locations
  hs_adm$address<-NA
  hs_adm$lat<-NA
  hs_adm$lng<-NA
  hs_adm$match_type<-NA



 #for (j in 1:50){
   for (j in 1:dim(hs_adm)[1]){
   tryCatch({
           if(j%%100==0){
              print(j)
            }
     temp<-google_find_place(hs_adm$school_name[j], fields = c("formatted_address","geometry"))
     #if no initial match
     if (is.null(temp$candidates$formatted_address)){

       temp<-google_find_place(hs_adm$school_name_long[j], fields = c("formatted_address","geometry"))
       if (is.null(temp$candidates$formatted_address)){
         temp<-google_find_place(hs_adm$school_name_town[j], fields = c("formatted_address","geometry"))
         if (is.null(temp$candidates$formatted_address)){
           hs_adm[j,]$match_type<-'unmatched'
           hs_adm[j,]$address<-NA
           hs_adm[j,]$lat<-NA
           hs_adm[j,]$lng<-NA
         } else {
           hs_adm[j,]$match_type<-'matched using only town name'
           hs_adm[j,]$address<-temp$candidates$formatted_address[1]
           hs_adm[j,]$lat<-temp$candidates$geometry$location$lat[1]
           hs_adm[j,]$lng<-temp$candidates$geometry$location$lng[1]
         }
       } else {
         hs_adm[j,]$match_type<-'matched using modified name'
         hs_adm[j,]$address<-temp$candidates$formatted_address[1]
         hs_adm[j,]$lat<-temp$candidates$geometry$location$lat[1]
         hs_adm[j,]$lng<-temp$candidates$geometry$location$lng[1]
       }
     } else {
       hs_adm[j,]$match_type<-'matched using name'
       hs_adm[j,]$address<-temp$candidates$formatted_address[1]
       hs_adm[j,]$lat<-temp$candidates$geometry$location$lat[1]
       hs_adm[j,]$lng<-temp$candidates$geometry$location$lng[1]
     }
   }, error=function(e){
     print("error")
     Sys.sleep(5)
     temp<-google_find_place(hs_adm$school_name[j], fields = c("formatted_address","geometry"))
     #if no initial match
     if (is.null(temp$candidates$formatted_address)){

       temp<-google_find_place(hs_adm$school_name_long[j], fields = c("formatted_address","geometry"))
       if (is.null(temp$candidates$formatted_address)){

         temp<-google_find_place(hs_adm$school_name_town[j], fields = c("formatted_address","geometry"))
         if (is.null(temp$candidates$formatted_address)){
           hs_adm[j,]$match_type<-'unmatched'
           hs_adm[j,]$address<-NA
           hs_adm[j,]$lat<-NA
           hs_adm[j,]$lng<-NA
         } else {
           hs_adm[j,]$match_type<-'matched using only town name'
           hs_adm[j,]$address<-temp$candidates$formatted_address[1]
           hs_adm[j,]$lat<-temp$candidates$geometry$location$lat[1]
           hs_adm[j,]$lng<-temp$candidates$geometry$location$lng[1]
         }
       } else {
         hs_adm[j,]$match_type<-'matched using name and county'
         hs_adm[j,]$address<-temp$candidates$formatted_address[1]
         hs_adm[j,]$lat<-temp$candidates$geometry$location$lat[1]
         hs_adm[j,]$lng<-temp$candidates$geometry$location$lng[1]
       }
     } else {
       hs_adm[j,]$match_type<-'matched using name'
       hs_adm[j,]$address<-temp$candidates$formatted_address[1]
       hs_adm[j,]$lat<-temp$candidates$geometry$location$lat[1]
       hs_adm[j,]$lng<-temp$candidates$geometry$location$lng[1]
     }
   })
 }


  #Add those HS that were already in HS BAC
  temp_hs_adm_in_hs_bac<-semi_join(hs_bac,hs_adm_all,by=c("school_name"="liceu_repartizat","judet")) %>% ungroup()

  hs_adm_final<-rbind(hs_adm,temp_hs_adm_in_hs_bac)
  rm(temp_hs_adm_in_hs_bac)

  #save hs_adm list
  setwd(wd_data_final)
  saveRDS(hs_adm_final, file = "hs_adm_locations")
  hs_adm<-readRDS("hs_adm_locations")
  
  #############################################
  ### Get MS locations
  ms_adm<-ms_adm %>% rename(school_name=scoala_de_provenienta)
  ms_adm<-ms_adm %>% filter(school_name!='')
  
  ms_adm$school_name_modified<-gsub('"(.*?)"',"",ms_adm$school_name,perl=T)
  ms_adm$school_name_modified<-gsub('SCOALA GIMNAZIALA',"SCOALA",ms_adm$school_name,perl=T)
  
  ms_adm$school_name_town<-gsub('"(.*?)"',"",ms_adm$school_name,perl=T)
  ms_adm$school_name_town<-gsub('SCOALA GIMNAZIALA',"SCOALA",ms_adm$school_name)
  
  ms_adm$address<-NA
  ms_adm$lat<-NA
  ms_adm$lng<-NA
  ms_adm$match_type<-NA
  
  ms_adm<-clean_get_addresses__get_town_from_list(ms_adm)
  
 
  #for (j in which(is.na(ms_adm$match_type))){
  for (j in 1:dim(ms_adm)[1]){
    tryCatch({
      if(j%%100==0){
        print(j)
      }
      temp<-google_find_place(ms_adm$school_name[j], fields = c("formatted_address","geometry"))
      #if no initial match
      if (is.null(temp$candidates$formatted_address)){
        
        temp<-google_find_place(ms_adm$school_name_modified[j], fields = c("formatted_address","geometry"))
          if (is.null(temp$candidates$formatted_address)){
          temp<-google_find_place(ms_adm$school_name_town[j], fields = c("formatted_address","geometry"))
          if (is.null(temp$candidates$formatted_address)){
            ms_adm[j,]$match_type<-'unmatched'
            ms_adm[j,]$address<-NA
            ms_adm[j,]$lat<-NA
            ms_adm[j,]$lng<-NA
          } else {
            ms_adm[j,]$match_type<-'matched using only town name'
            ms_adm[j,]$address<-temp$candidates$formatted_address[1]
            ms_adm[j,]$lat<-temp$candidates$geometry$location$lat[1]
            ms_adm[j,]$lng<-temp$candidates$geometry$location$lng[1]
          }
        } else {
        ms_adm[j,]$match_type<-'matched using modified name'
        ms_adm[j,]$address<-temp$candidates$formatted_address[1]
        ms_adm[j,]$lat<-temp$candidates$geometry$location$lat[1]
        ms_adm[j,]$lng<-temp$candidates$geometry$location$lng[1]
        }
      } else {
        ms_adm[j,]$match_type<-'matched using name'
        ms_adm[j,]$address<-temp$candidates$formatted_address[1]
        ms_adm[j,]$lat<-temp$candidates$geometry$location$lat[1]
        ms_adm[j,]$lng<-temp$candidates$geometry$location$lng[1]
      }
    }, error=function(e){
      print("error")
      Sys.sleep(10)
      temp<-google_find_place(ms_adm$school_name[j], fields = c("formatted_address","geometry"))
      #if no initial match
      if (is.null(temp$candidates$formatted_address)){
        
        temp<-google_find_place(ms_adm$school_name_modified[j], fields = c("formatted_address","geometry"))
        if (is.null(temp$candidates$formatted_address)){
          temp<-google_find_place(ms_adm$school_name_town[j], fields = c("formatted_address","geometry"))
          if (is.null(temp$candidates$formatted_address)){
            ms_adm[j,]$match_type<-'unmatched'
            ms_adm[j,]$address<-NA
            ms_adm[j,]$lat<-NA
            ms_adm[j,]$lng<-NA
          } else {
            ms_adm[j,]$match_type<-'matched using only town name'
            ms_adm[j,]$address<-temp$candidates$formatted_address[1]
            ms_adm[j,]$lat<-temp$candidates$geometry$location$lat[1]
            ms_adm[j,]$lng<-temp$candidates$geometry$location$lng[1]
          }
        } else {
          ms_adm[j,]$match_type<-'matched using modified name'
          ms_adm[j,]$address<-temp$candidates$formatted_address[1]
          ms_adm[j,]$lat<-temp$candidates$geometry$location$lat[1]
          ms_adm[j,]$lng<-temp$candidates$geometry$location$lng[1]
        }
      } else {
        ms_adm[j,]$match_type<-'matched using name'
        ms_adm[j,]$address<-temp$candidates$formatted_address[1]
        ms_adm[j,]$lat<-temp$candidates$geometry$location$lat[1]
        ms_adm[j,]$lng<-temp$candidates$geometry$location$lng[1]
      }
    })
  }
  
  #save ms_adm list
  # ms_adm_1<-readRDS("ms_adm_locations_1") %>% filter(!is.na(match_type))
  # ms_adm_2<-readRDS("ms_adm_locations_2") %>% filter(!is.na(match_type))
  # ms_adm_3<-readRDS("ms_adm_locations_3") %>% filter(!is.na(match_type))
  # ms_adm<-rbind(ms_adm_1,ms_adm_2,ms_adm_3)
  # ms_adm<-unique(ms_adm)
  # ms_adm_all<-unique(ms_adm_all)
  
 
  #Add those MS that were already in HS BAC and HS ADM
  temp_ms_adm_in_hs_bac<-semi_join(hs_bac,ms_adm_all,by=c("school_name"="scoala_de_provenienta","judet"))
  colnames(temp_ms_adm_in_hs_bac)[colnames(temp_ms_adm_in_hs_bac)=='unitate_de_invatamant']<-'school_name'
  
  temp_ms_adm_in_hs_adm<-semi_join(hs_adm,ms_adm_all,by=c("school_name"="scoala_de_provenienta","judet"))
  colnames(temp_ms_adm_in_hs_adm)[colnames(temp_ms_adm_in_hs_adm)=='liceu_repartizat']<-'school_name'
  
  
  ms_adm<-rbind(ms_adm,temp_ms_adm_in_hs_bac,temp_ms_adm_in_hs_adm)
  ms_adm<-unique(ms_adm)
  rm(temp_ms_adm_in_hs_bac,temp_ms_adm_in_hs_adm)
  
  #save ms_adm list
  setwd(wd_data_final)
  saveRDS(ms_adm, file = "ms_adm_locations")
  ms_adm<-readRDS(ms_adm, file = "ms_adm_locations")
  

  
  ms_adm<-unique(ms_adm) %>% group_by(judet,school_name) %>% mutate(n=n()) %>% filter(n==1 | (n>1) & !grepl('"',school_name)) %>% arrange(judet,school_name) %>% ungroup()
  
  gc()


}